<template>
    <div class="login-wrap">
        <div class="bgView">
        </div>
        <div class="ms-login">
            <div class="ms-title">
                <img src="../../assets/ryzLogo/logo-1.png" class="logo" />
                <!-- <div class="title">后台管理系统</div> -->
            </div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="mobile">
                    <el-input v-model="param.mobile" placeholder="请输入登录账号">
                        <el-button slot="prepend" icon="el-icon-user"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入登录密码" v-model="param.password" @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="warning" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
    import md5 from 'js-md5'
    export default {
        data: function() {
            return {
                param: {
                    mobile: '',
                    password: '',
                },
                rules: {
                    mobile: [{ required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 11, max: 11, message: '1开头的11位手机号码', trigger: 'blur' }
                    ],
                    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                },
            };
        },
        mounted() {
            console.log(this.$store.state.user.count, 55555);
        },
        methods: {
            submitForm() {
                this.$refs.login.validate(valid => {
                    if (valid) {
                        let params = {
                            mobile: this.param.mobile,
                            password: md5(this.param.password)
                        }
                        this.$api.partnerLogin(params)
                            .then(res => {
                                console.log('登录数据', res);
                                if (res.code == 1) {
                                    let token = res.data.token.access_token
                                    let user_id = JSON.stringify(res.data.user_id)
                                    let sid = JSON.stringify(res.data.sid)
                                    let role = res.data.role
                                    let info = JSON.stringify(res.data.info)
                                    let count = res.data.count
                                    //let username = JSON.stringify(res.data.name)
                                    localStorage.setItem('liankun_user', token);
                                    localStorage.setItem('user_id', user_id);
                                    localStorage.setItem('info', info);
                                    this.$message.success('登录成功');
                                    localStorage.setItem('sid', sid);
                                    localStorage.setItem('role', role);
                                    localStorage.setItem('count', count);
                                    localStorage.setItem('mobile', this.param.mobile);
                                    localStorage.setItem('sale_type', res.data.sale_type);
                                    localStorage.setItem('market_id',res.data.market_id)
									localStorage.setItem('packAuth',res.data.is_pk_auth)
                                    if (count > 1) {
                                        this.$router.push('/selectStore')
                                    } else {
                                        this.$router.push('/manaindex')
                                    }


                                    //localStorage.setItem('username', username);
                                    // let params = {
                                    //   type: '',//menu-菜单 api-接口 不传则返回全部
                                    //   output: 'tree',//不传就是正常结构，传tree就是树形结构
                                    //   user_id: user_id,
                                    //   access_token: access_token
                                    // }
                                    // this.$api.getPermissions(params).then(res => {
                                    //   console.log(res, 666666);
                                    // })
                                } else {
                                    this.$message.error('用户账号或密码错误');
                                }

                            })
                            .catch(err => {
                                this.$message.error(err);
                            })
                    } else {
                        this.$message.error('请输入账号和密码');
                        return false;
                    }
                });
            },
        },
    };
</script>

<style scoped lang='less'>
    .login-wrap {
        position: relative;
        width: 100%;
        height: 100%;
        background-image: url(../../assets/bg.jpg);
        background-repeat: no-repeat;
        background-size: 100% 100%;
    }

    .bgView {
        position: absolute;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, .6);
    }

    .ms-title {
        width: 100%;
        // line-height: 50px;
        /* text-align: center; */
        font-size: 20px;
        color: #fff;
        border-bottom: 1px solid #ddd;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding:20px;

        .logo {
            width:240px;
            height: 80px;

        }

        .title {
            color: #fff;
            font-weight: 600;
            font-size: 16px;
        }
    }

    .ms-login {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 450px;
        margin: -190px 0 0 -175px;
        border-radius: 5px;
        background: rgba(255, 255, 255, 0.1);
        overflow: hidden;
    }

    .ms-content {
        padding: 30px 45px;
    }

    .login-btn {
        text-align: center;
    }

    .login-btn button {
        width: 100%;
        height: 36px;
        margin-bottom: 10px;
    }
</style>